package org.example.simple_1.web;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Map;

@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @PostMapping("/{orderId}/event")
    public Mono<String> sendEvent(@PathVariable String orderId, @RequestBody Map<String, String> payload) {
        String event = payload.get("event");
        try {
            orderService.handleEvent(orderId, event);
            return Mono.just("Event " + event + " processed.");
        } catch (Exception e) {
            return Mono.error(new RuntimeException("Error: " + e.getMessage()));
        }
    }

    @GetMapping("/{orderId}/state")
    public Mono<String> getState(@PathVariable String orderId) {
        return Mono.just(orderService.getCurrentState(orderId));
    }
}